package com.edu.zju.scs.panacea.controller.exposure;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.exposure.TerminalInfo;
import com.edu.zju.scs.panacea.entity.exposure.UploadData;
import com.edu.zju.scs.panacea.service.exposure.ExposureService;
import com.edu.zju.scs.panacea.utils.DateUtil;
import com.edu.zju.scs.panacea.utils.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2019-02-08 10:57
 */
@Controller
@EnableAutoConfiguration
public class WifiDataController {

    @Autowired
    private ExposureService wifiDataService;


    @ResponseBody
    @ApiOperation(value="查询某个计划的投放效果", notes="查询某个计划的投放效果")
    @RequestMapping(value = "/plan/launch/consumer", method = RequestMethod.POST)
    public BaseResult getConsumerData(
            @ApiParam("设备ID") @RequestParam(value = "device_id") String deviceId,
            @ApiParam("上传时间") @RequestParam(value = "device_time") String deviceTime,
            @ApiParam("信息") @RequestParam(value = "message") String message) {
//        System.out.println("########################################################");
        UploadData uploadData = new UploadData();
        // DC4F22888612
        uploadData.setDeviceId(deviceId);
        uploadData.setTransmitTime(DateUtil.toDate(deviceTime));
        uploadData.setTerminalInfos(getTerminalsFromString(message));
        uploadData.setTaskId(83L);
//        System.out.println(uploadData);
        wifiDataService.handleWifiData(uploadData);
        return new BaseResult();
    }

    private List<TerminalInfo> getTerminalsFromString(String message) {
        String[] terminalInfoStrings = message.split("\n");
        List<TerminalInfo> terminalInfos = new ArrayList<>();
        for (String terminalInfoString : terminalInfoStrings) {
            TerminalInfo terminalInfo = new TerminalInfo();
            String[] columns = terminalInfoString.split(",");
            terminalInfo.setTerminalMac(StringUtil.isTrimEmpty(columns[0]) ? null : columns[0]);
            terminalInfo.setRouteMac(StringUtil.isTrimEmpty(columns[1]) ? null : columns[1]);
            terminalInfo.setRouteName(StringUtil.isTrimEmpty(columns[2]) ? null : columns[2]);
            terminalInfo.setDistance(StringUtil.isTrimEmpty(columns[3]) ? null : Float.parseFloat(columns[3]));
            terminalInfo.setConnected(StringUtil.isTrimEmpty(columns[4]) ? null : Boolean.parseBoolean(columns[4]));
            terminalInfos.add(terminalInfo);
        }
        TerminalInfo terminalInfoa = new TerminalInfo();
        terminalInfoa.setTerminalMac("0C:72:2C:36:D0:92");
        terminalInfoa.setDistance(30.8f);
        terminalInfos.add(terminalInfoa);
        return terminalInfos;
    }
}
